import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:xinxuan/config/app_config.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';
import 'package:r_upgrade/r_upgrade.dart';

class UploadApkDialog extends Dialog {
  final String apkUrl;
  final String newFeature;
  final String versionName;
  final int upgradeType;
  UploadApkDialog(this.apkUrl, this.newFeature, this.versionName, this.upgradeType);
  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: 320.w,
          child: Stack(
            children: [
              Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(height: 90.h),
                  Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(8.w),
                      color: bgWhiteColor
                    ),
                    child: Column(
                      children: [
                        SizedBox(height: 87.h),
                        Container(
                          padding: EdgeInsets.symmetric(vertical: 12.h, horizontal: 20.w),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '更新内容:',
                                style: TextStyle(
                                  color: textBlackColor,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.bold
                                ),
                              ),
                              HtmlWidget(
                                '$newFeature'
                              )
                            ],
                          ),
                        ),
                        Container(
                          height: 54.h,
                          decoration: BoxDecoration(
                            border: Border(
                              top: BorderSide(
                                width: .5,
                                color: lineColor
                              )
                            )
                          ),
                          child: Row(
                            children: [
                              InkWellUtil.shakeProofInkWell(Container(
                                width: 160.w,
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  border: Border(
                                    right: BorderSide(
                                      width: .5,
                                      color: lineColor
                                    )
                                  )
                                ),
                                child: Text(
                                  '暂不更新',
                                  style: TextStyle(
                                    color: textSubtitleColor,
                                    fontSize: 16.sp
                                  ),
                                ),
                              ), () {
                                if (upgradeType == 1) {
                                  exit(0);
                                } else {
                                  Get.back();
                                }
                              },
                              hoverColor: Colors.transparent,
                              splashColor: Colors.transparent,
                              highlightColor: Colors.transparent),
                              InkWellUtil.shakeProofInkWell(Container(
                                width: 159.w,
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  border: Border(
                                    right: BorderSide(
                                      width: .5,
                                      color: lineColor
                                    )
                                  )
                                ),
                                child: Text(
                                  '立即更新',
                                  style: TextStyle(
                                    color: mainTextColor,
                                    fontSize: 16.sp
                                  ),
                                ),
                              ), () {
                                if(Platform.isIOS){
                                  RUpgrade.upgradeFromAppStore(AppConfig.appStoreId);
                                }else if(Platform.isAndroid){
                                  if (upgradeType != 1) {
                                    Get.back();
                                  }
                                  commonPageController.downloadApk(apkUrl, versionName, showLoading: upgradeType == 1);
                                }
                              },
                              hoverColor: Colors.transparent,
                              splashColor: Colors.transparent,
                              highlightColor: Colors.transparent)
                            ],
                          ),
                        )
                      ],
                    ),
                  )
                ],
              ),
              Positioned(
                top: 0,
                left: 0,
                child: Container(
                  width: 320.w,
                  height: 177.h,
                  alignment: Alignment.bottomCenter,
                  padding: EdgeInsets.only(bottom: 16.h),
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage(
                        'assets/icons/icon_upload_apk.png',
                      ),
                      fit: BoxFit.fill
                    )
                  ),
                  child: Container(
                    alignment: Alignment.bottomCenter,
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 3.h),
                      constraints: BoxConstraints(
                        minWidth: 163.w,
                      ),
                      decoration: BoxDecoration(
                        color: bgWhiteColor,
                        borderRadius: BorderRadius.circular(15.w),
                        border: Border.all(
                          width: 1.w,
                          color: bgMainColor
                        )
                      ),
                      child: Text(
                        '发现新版本:V$versionName',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: mainTextColor,
                          fontSize: 18.sp
                        ),
                      ),
                    ),
                  ),
                )
              )
            ],
          ),
        ),
      ),
    );
  }
}